<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; 
use App\Member;
use App\Realname;
use App\AccountRecharge;
use App\Withdraw;
use App\AmountQuoteLog;
use App\QuoteBillMonth;
use App\Product_order;

Class UserController extends AdminController{
    
    /**
     * 首页概览数据
     * @param Request $request
     */
    public function overview(Request $request){
        $data = array(
            'reg'=>0,
            'bill'=>0,
            'recharge'=>0,
            'withraw'=>0,
            'quote'=>0,
            'shenqing'=>0,
            'overdue'=>0
        );
        $today = strtotime(date('Y-m-d'));
        //注册
        $reg = Member::where('register_time','>=',  $today)->count();
        if($reg){$data['reg']=$reg;}
        //交易流水
        $orderamount = Product_order::withTrashed()->where('payment_time','>=',  $today)->sum('product_price');
        if($orderamount){$data['bill']=$orderamount;}
        //充值
        $chongzhi = AccountRecharge::where('add_time','>=',  $today)->where('status', 1)->sum('amount');
        if($chongzhi){$data['recharge']=$chongzhi;}
        //提现
        $tixian = Withdraw::where('created_at','>=',  $today)->where('status', 1)->sum('amount');
        if($tixian){$data['withraw']=$tixian;}
        //消费金融
        $quote = AmountQuoteLog::where('add_time','>=',  $today)->where('type', 1)->sum('amount');
        if($quote){$data['quote']=$quote;}
        //提现审核
        $tixianShenhe = Withdraw::where('status', -2)->count();
        if($tixianShenhe){$data['shenqing']=$tixianShenhe;}
        //逾期
        $overdue = QuoteBillMonth::where('status', 3)->count();
        if($overdue){$data['overdue']=$overdue;}
        $this->output($data);
    }
    
    /**
     * 全部用户
     * @param Request $request
     */
    public function userList(Request $request){
        $name = trim($request->input('name', ''));
        $phone = trim($request->input('phone', ''));
        $idcard = trim($request->input('idcard', ''));
         
        $page=trim($request->input('page', 0));//页码
        $pagenumber=trim($request->input('pagenumber', '30'));//分页个数
        $sql = "select c.realname,a.phone,a.id,c.card_id from mega_member as a left join mega_realname as c on a.id = c.member_id where 1 ";
        $sqlcount = "select count(a.id) as count from mega_member as a left join mega_realname as c on a.id = c.member_id where 1 ";
        $param = array();
        $sqlwhere = '';
        if($idcard){
            $sqlwhere.=" and c.card_id=:card_id ";
            $param[':card_id']= $idcard;
        }
        if($name){
            $name = "%$name%";
            $sqlwhere.=" and c.realname like :realname ";
            $param[':realname']= $name;
        }
        if($phone){
            $sqlwhere.=" and a.phone=:phone ";
            $param[':phone']= $phone;
        }
        
        $start = $page * $pagenumber;
        $limit = " order by a.id desc limit $start, $pagenumber ";
        $results = DB::select($sql. $sqlwhere. $limit, $param);
        $count = DB::select($sqlcount.$sqlwhere, $param);
        
        $data = array(
            'count'=>$count[0]->count,
            'items'=>$results,
            'page'=>$page
        );
        
        $this->output($data);
    }
    
    /**
     * 全部详细信息
     * @param Request $request
     */
    public function userDetail(Request $request){
        $id =  trim($request->input('id', ''));
        $member = Member::where('id', $id)->first();
        $realname = Realname::where('member_id', $id)->first();
        $info =array(
            'realname'=>'',
            'phone'=>'',
            'card_id'=>'',
            'reg_date'=>'',
        );
        if($member){
            $info['reg_date'] = date('Y-m-d', $member->register_time);
            $info['phone'] = $member->phone;
        }
        if($realname){
            $info['card_id'] = $realname->card_id;
            $info['realname'] = $realname->realname;
        }
        $this->output($info);
    }
    
    //mega_member_bank
    /**
     * 用户银行卡
     * @param Request $request
     */
    public function bankList(Request $request){
        $name = trim($request->input('name', ''));
        $phone = trim($request->input('phone', ''));
        $idcard = trim($request->input('idcard', ''));
        $bankcard = trim($request->input('bankcard', ''));
        $bankname = trim($request->input('bankname', ''));
        $page=trim($request->input('page', 0));//页码
        $pagenumber=trim($request->input('pagenumber', '30'));//分页个数
        $sql = "select c.realname,a.phone,c.id,c.card_id,b.card_name,b.card_no as bankcard, b.created_at  from mega_member as a inner join mega_member_bank as b on a.id = b.member_id left join mega_realname as c on a.id = c.member_id where 1 ";
        $sqlcount = "select count(a.id) as count from mega_member as a inner join mega_member_bank as b on a.id = b.member_id left join mega_realname as c on a.id = c.member_id where 1 ";
        $param = array();
        $sqlwhere = '';
        if($idcard){
            $sqlwhere.=" and c.card_id=:card_id ";
            $param[':card_id']= $idcard;
        }
        if($name){
            $name = "%$name%";
            $sqlwhere.=" and c.realname like :realname ";
            $param[':realname']= $name;
        }
        if($phone){
            $sqlwhere.=" and a.phone=:phone ";
            $param[':phone']= $phone;
        }
        if($bankcard){
            $sqlwhere.=" and b.card_no=:card_no ";
            $param[':card_no']= $bankcard;
        }
        if($bankname){
            $bankname = "%$bankname%";
            $sqlwhere.=" and b.card_name like :bankname ";
            $param[':bankname']= $bankname;
        }
        $start = $page * $pagenumber;
        $limit = " order by a.id desc limit $start, $pagenumber ";
        $results = DB::select($sql. $sqlwhere. $limit, $param);
        $count = DB::select($sqlcount.$sqlwhere, $param);
        
        $data = array(
            'count'=>$count[0]->count,
            'items'=>$results,
            'page'=>$page
        );
        
        $this->output($data);
    }
}
